home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / OpenInventorLab / lab / walk2.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.6 KB  |  96 lines

  1. //-----------------------------------------------------------------------
  2. //
  3. //  Inventor Barcelona Lab #2
  4. //
  5. //    This program illustrates a simple Inventor program which reads
  6. //    in an Inventor file and views it with the `Walkthrough viewer'.
  7. //    If the `Table' is picked then play some audio...
  8. //
  9. //    EXERCISE:
  10. //    Modify the program so that picking the `Lamp' causes
  11. //      ./sounds/lampON.aiff  and ./sounds/lampOFF.aiff to play.
  12. //      Alternate these two sounds each time.
  13. //
  14. //     (Hint: you will need to create a boolean flag for the lamp status...)
  15. //
  16. //    Type: make walk2
  17. //
  18. //-----------------------------------------------------------------------
  19.  
  20. #include <stdio.h>
  21. #include <Inventor/SoDB.h>
  22. #include <Inventor/actions/SoSearchAction.h>
  23. #include <Inventor/events/SoMouseButtonEvent.h>
  24. #include <Inventor/nodes/SoEventCallback.h>
  25. #include <Inventor/nodes/SoSeparator.h>
  26. #include <Inventor/Xt/SoXt.h>
  27. #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
  28.  
  29.  
  30. void tableCallback( void *, SoEventCallback *cb ) {
  31.     // Make sure that this is a MOUSE PRESS event
  32.     if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
  33.  
  34.     system( "playaiff sounds/table.aiff &" );
  35. }
  36.  
  37.  
  38. void tableInit( SoSeparator *root )
  39.     // Find the `Table' node
  40.     SoGroup *table = (SoGroup *) root->getByName( "Table" );
  41.  
  42.     if ( (table == NULL) || (!table->isOfType( SoGroup::getClassTypeId())) ) {
  43.     printf( "DEBUG: `Table' not found or wrong type\n" );
  44.     exit( 0 );
  45.     }
  46.  
  47.     // Get the path to `Table'
  48.     SoSearchAction sa;
  49.     sa.setFind( SoSearchAction::NODE );
  50.     sa.setNode( table );
  51.     sa.apply( root );
  52.     SoPath *path = sa.getPath();
  53.  
  54.     // Create an event callback node that calls a function if the
  55.     // `Table' is picked.
  56.  
  57.     SoEventCallback *tableCB = new SoEventCallback;
  58.     tableCB->setPath(path);
  59.     tableCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
  60.                 tableCallback );
  61.     table->addChild( tableCB );
  62. }
  63.  
  64.  
  65. main(int argc, char **argv)
  66. {
  67.     // Initialize Inventor
  68.     Widget myWindow = SoXt::init( argv[0] );
  69.     if (myWindow == NULL) exit(1);
  70.  
  71.     // Make a viewer part of the window
  72.     SoXtWalkViewer *viewer = new SoXtWalkViewer(myWindow);
  73.  
  74.     // Read the object from a file
  75.     SoInput myInput;
  76.     if (!myInput.openFile("./scene.iv")) return(1);
  77.     SoSeparator *scene = SoDB::readAll(&myInput);
  78.     if (scene == NULL) {
  79.     printf( "Error: scene.iv file read failed!\n" );
  80.     exit(1);
  81.     }
  82.     scene->ref();
  83.  
  84.     // Search for the different objects by name
  85.     tableInit( scene );
  86.  
  87.     // Viewer setup
  88.     viewer->setSceneGraph( scene );
  89.     viewer->setTitle( "Walkthrough Program" );
  90.     viewer->show();
  91.     SoXt::show(myWindow);
  92.  
  93.     SoXt::mainLoop();
  94. }
  95.